"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const utils = require("utils");
const request = require("request");
const parse = require("./parse.js");
const tool = require("./tool.js");
const formatterContent = require("../formatterContent.js");
async function requestData(url) {
  return await request.getRequest(url)(
    {},
    {
      dataType: "",
      headers: {
        DNT: 1,
        Pragma: "no-cache"
      }
      // enableProxy: true,
      // rejectUnauthorized: false,
      // proxy: "http://127.0.0.1:8899",
    }
  );
}
async function handleResultBuffer(buffer, salt, secret) {
  try {
    let decodeBuffer = await parse.decode(buffer, salt, secret);
    let transform = new Uint8Array(decodeBuffer);
    let dataViewData = tool(void 0).formatterData(transform, void 0);
    let dataView = new DataView(dataViewData.buffer, 0, dataViewData.buffer.byteLength);
    const textDecoder = new TextDecoder();
    let result = textDecoder.decode(dataView);
    return [void 0, result];
  } catch (error) {
    return [new utils.InsideErr(error), void 0];
  }
}
async function toRequestSecretData(secretInfo, ecpOssUrl) {
  let { salt, secret } = secretInfo || {};
  if (utils.util.nilValue(salt)) {
    return [new utils.ParamErr(" secretInfo的 salt "), void 0];
  }
  if (utils.util.nilValue(secret)) {
    return [new utils.ParamErr(" secretInfo的 secret "), void 0];
  }
  if (utils.util.nilValue(ecpOssUrl)) {
    return [new utils.ParamErr(" ecpOssUrl "), void 0];
  }
  let [file_err, file_data] = await requestData(ecpOssUrl);
  if (!utils.util.nilValue(file_err)) {
    return [file_err, void 0];
  }
  let [content_err, content_data] = await handleResultBuffer(file_data, salt, secret);
  if (!utils.util.nilValue(content_err)) {
    return [content_err, void 0];
  }
  return formatterContent.formatterContent(content_data);
}
exports.toRequestSecretData = toRequestSecretData;
